#!/usr/bin/env python3
# Author:liang futing
#@Time :2020/3/13 10:44
from django import template
from django.contrib.auth import get_user_model
from django.db.models import Count
from django.db.models.functions import TruncMonth
from blog.models import Blog,Article,Category,Tag
User=get_user_model()

register = template.Library()

#inclusion_tag 可以引入一套模板语法进行渲染,将数据与样式结合为一体
@register.inclusion_tag('classification.html')
def get_classification_style(username):
    user = User.objects.filter(username=username).first()
    if not user:
        return None
    blog = user.blog
    article_list = Article.objects.filter(user=user).all()
    cate_list = Category.objects.filter(blog=blog).values("pk"). \
        annotate(c=Count("article__title")).values_list("title", 'c')
    # print(article,blog)
    tag_list = Tag.objects.filter(blog=blog).values("pk"). \
        annotate(c=Count("article__title")).values_list("title", 'c')
    article_month_count = article_list.annotate(month=TruncMonth('create_time')).values('month'). \
        annotate(c=Count('nid')).values_list('month', 'c').order_by()
    return {'blog': blog, 'user': user, 'article_list': article_list, 'tag_list': tag_list, 'cate_list': cate_list,
            'article_month_count': article_month_count}

